import * as request from "request";
import { Repo } from "./model/Repo";
import { User } from "./model/User";

const options = {
  headers: {
    "User-Agent": "request",
  },
  json: true,
};

export class GithubApiService {
  getUserInfo(username: string, cb: any) {
    request.get(
      "https://api.github.com/users/" + username,
      options,
      (error: any, response: any, body: any) => {
        // console.log(body);

        let user: User = new User(body);
        // console.log(user);
        cb(user);
      }
    );
  }

  getRepo(username: string, cb: any) {
    request.get(
      "https://api.github.com/users/" + username + "/repos",
      options,
      (error: any, response: any, body: any) => {
        // let repo: Repo = new Repo(body);
        let repos: Repo[] = body.map((item: Repo) => new Repo(item));
        // console.log(user);
        cb(repos);
      }
    );
  }
}
